<template>
  <main-content title="商品分类">
    <template #actions-bar>
      <el-button
        v-has="['08030201']"
        type="primary"
        @click="goAddCate"
      >
        添加商品分类
      </el-button>
    </template>

    <el-table
      v-loading="loading"
      :data="goodsCateList"
    >
      <el-table-column
        prop="id"
        label="编号"
        min-width="60"
      />
      <el-table-column
        prop="name"
        label="分类名称"
        min-width="150"
      />
      <el-table-column
        prop="goodsAmount"
        label="商品数量"
        min-width="150"
      />
      <el-table-column
        prop="status"
        label="是否显示"
        min-width="150"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            inline-prompt
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="0"
            @change="(val) => handleCateStatus(val, scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        min-width="150"
      />
      <el-table-column
        label="操作"
        width="100"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            v-has="['08030202']"
            link
            type="primary"
            @click="editCate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-has="['08030203']"
            link
            type="danger"
            @click="deleteCate(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </main-content>
</template>
<script lang="ts">
export default {
  name: "MallProductCategory",
};
</script>
<script setup lang="ts">
import { IGoodsCateItem } from "@/api/modules/pointsMall";
import {
  deleteGoodCategory,
  getGoodsCategory,
  updateCategoryStatus,
} from "@/api/pointsMall";
import { ElDialog, ElMessageBox } from "element-plus";
import { useMallConfig } from "@/views/pointsMall/compositions/useMallConfig";
import {useCrossPage} from "@/compositions/crossPage/useCrossPage";

const router = useRouter();
const { mallConfig } = useMallConfig();

const handleCateStatus = async (val: boolean, row: IGoodsCateItem) => {
  const rs = await updateCategoryStatus({ id: row.id, name: row.name });

  if (rs.code === 200) {
    ElMessage.success("更新成功");
  }
};

const loading = ref(false);
const goodsCateList = ref<IGoodsCateItem[]>([]);
const _getGoodsCateList = async () => {
  loading.value = true;
  const rs = await getGoodsCategory({ id: mallConfig.value.id });
  loading.value = false;
  goodsCateList.value = rs.payload ?? [];
};

const refreshList = () => {
  _getGoodsCateList()
}

const goAddCate = () => {
  router.push("/mall/product/category/add_goods_cate");
};

const deleteCate = (row: IGoodsCateItem) => {
  ElMessageBox.confirm("确定删除此分类？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(async () => {
    const rs = await deleteGoodCategory({ id: row.id });

    if (rs.code === 200) {
      ElMessage.success("删除成功");
      await _getGoodsCateList();
    }
  });
};

const editCate = (row: IGoodsCateItem) => {
  router.push({
    path: "/mall/product/category/add_goods_cate",
    query: {
      id: row.id,
    },
  });
};
const store = useStore();
// 监听机构
const insId = computed(() => {
  return store.state.insId.insId;
});
watch(
  () => insId,
  () => {
    _getGoodsCateList();
  },
  {
    deep: true,
  }
);

useCrossPage("pointsMall", {refreshList})

onMounted(() => {
  _getGoodsCateList();
});
</script>

<style scoped lang="scss"></style>
